package com.alibaba.shopping_admin_service.service;

import com.alibaba.shopping_admin_service.mapper.RoleMapper;
import com.alibaba.shopping_common.pojo.Role;
import com.alibaba.shopping_common.service.RoleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName RoleServiceImpl
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/6 12:43
 */
@Transactional // 事务管理
@DubboService // 声明是一个dubbo服务
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void addRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void delRoleByID(Long rid) {
        roleMapper.deleteById(rid);
    }

    @Override
    public void updateRoleByID(Role role) {
        // roleMapper.update(role,null)
        roleMapper.updateById(role);
    }

    @Override
    public Role findByID(Long rid) {
        return roleMapper.findByID(rid);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public Page<Role> findRoleByPage(int page, int size) {
        return roleMapper.selectPage(new Page<>(page,size),null);
    }

    @Override
    public void updatePermissionToRole(Long rid, Long[] pids) {
        // 1. 删除角色所有的权限
        roleMapper.delRolePermission(rid);
        // 2. 给角色新增权限
        for (Long pid:pids){
            roleMapper.addRolePermission(rid,pid);
        }
    }
}




